package cn.err0r.android.shsmead;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;


@SuppressWarnings("deprecation")
public class ViewScroll extends AbsoluteLayout
{
	private int screenW;	
	private int screenH;	
	private int imgW;		
	private int imgH;		
	private TouchView tv;
	private Context mContext;
	ProgressDialog dialog;
	public ViewScroll(Context context,String resId,View topView)
	{
		super(context);
		screenW = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
		screenH = ((Activity)context).getWindowManager().getDefaultDisplay().getHeight()-(topView==null?190:topView.getBottom()+50);
		tv = new TouchView(context,screenW,screenH);
		String url = resId;
		mContext = context;
		AQuery aq = new AQuery(context);
		dialog = new ProgressDialog(context);

		dialog.setIndeterminate(true);
		dialog.setCancelable(true);
		dialog.setInverseBackgroundForced(false);
		dialog.setCanceledOnTouchOutside(true);
		dialog.setTitle("图片加载中...");

		aq.progress(dialog).ajax(url, Bitmap.class, new AjaxCallback<Bitmap>() {

		        @Override
		        public void callback(String url, Bitmap object, AjaxStatus status) {
		        	Log.d("IMG", url);
		        	if(status.getCode() == 200){
			        	tv.setImageBitmap(object);
			            Bitmap img = object;
			            imgW = img.getWidth();
			            imgH = img.getHeight();
			            int layout_w = imgW>screenW?screenW:imgW; 
			            int layout_h = imgH>screenH?screenH:imgH; 
			            if(layout_w==screenW||layout_h==screenH)
			            	tv.setScaleType(ScaleType.FIT_XY);
			            tv.setLayoutParams(new AbsoluteLayout.LayoutParams(layout_w,layout_h , layout_w==screenW?0:(screenW-layout_w)/2, layout_h==screenH?0:(screenH-layout_h)/2));
		        	}else{
		        		Toast.makeText(mContext, "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
		        	}
		                
		        }
		});
		this.addView(tv);
		
        
	}

	
}
